What is hyperid?
The hyperid npm package is a fast and efficient library for generating unique identifiers. It is designed to be lightweight and performant, making it suitable for use in high-throughput environments.
What are hyperid's main functionalities?
Generate Unique IDs
This feature allows you to generate unique identifiers. The `hyperid` function creates an instance, and calling this instance generates a unique ID.
const hyperid = require('hyperid');
const instance = hyperid();
const id = instance();
console.log(id);
Generate IDs with Fixed Prefix
This feature allows you to generate unique identifiers with a fixed length. This can be useful when you need IDs of a consistent size.
const hyperid = require('hyperid');
const instance = hyperid({ fixedLength: true });
const id = instance();
console.log(id);
Decode IDs
This feature allows you to decode a generated ID back into its constituent parts. This can be useful for debugging or for extracting information from the ID.
const hyperid = require('hyperid');
const instance = hyperid();
const id = instance();
const decoded = instance.decode(id);
console.log(decoded);
Other packages similar to hyperid
uuid
The uuid package is a popular library for generating UUIDs (Universally Unique Identifiers). It supports multiple versions of UUIDs, including v1, v3, v4, and v5. Compared to hyperid, uuid is more versatile in terms of the types of UUIDs it can generate, but it may not be as performant in high-throughput scenarios.
nanoid
The nanoid package is a small, secure, URL-friendly unique string ID generator. It is designed to be fast and efficient, similar to hyperid. However, nanoid focuses on generating shorter, URL-friendly IDs, whereas hyperid provides more flexibility in terms of ID structure and decoding capabilities.
shortid
The shortid package is a library for generating short, non-sequential unique IDs. It is designed to be simple and easy to use. Compared to hyperid, shortid generates shorter IDs, but it does not offer the same level of performance or the ability to decode IDs.
hyperid

Uber-fast unique id generation, for Node.js and the browser.
Here are the benchmarks:
hashids process.hrtime x 161,779 ops/sec ±3.25% (78 runs sampled)
hashids counter x 327,099 ops/sec ±2.45% (76 runs sampled)
shortid x 26,225 ops/sec ±3.90% (72 runs sampled)
nid x 864,834 ops/sec ±2.94% (83 runs sampled)
uuid.v4 x 269,960 ops/sec ±2.42% (81 runs sampled)
uuid.v1 x 1,687,472 ops/sec ±1.38% (85 runs sampled)
hyperid - variable length x 6,121,953 ops/sec ±2.66% (76 runs sampled)
hyperid - fixed length x 6,331,137 ops/sec ±2.31% (79 runs sampled)
Note: Benchmark run with 1,3 GHz Intel Core i5 using Node v8.10.0
Install
npm i hyperid --save
Example
'use strict'
const hyperid = require('hyperid')
const instance = hyperid()
const id = instance()
console.log(id)
console.log(instance())
console.log(hyperid.decode(id))
console.log(hyperid.decode(instance()))
API
hyperid([fixedLength || options])
Returns a function to generate unique ids.
The function can accept one of the following parameters:
fixedLength: Boolean
If fixedLength is true
the function will always generate an id
that is 33 characters in length, by default fixedLength
is false
.
options: Object
If { fixedLength: true }
is passed in, the function will always generate an id
that is 33 characters in length, by default fixedLength
is false
.
If { urlSafe: true }
is passed in, the function will generate url safe ids.
instance()
Returns an unique id.
instance.uuid
The uuid used to generate the ids, it will change over time.
It is regenerated every Math.pow(2, 31) - 1
to keep the integer a SMI
(a V8 optimization).
hyperid.decode(id, [options])
Decode the unique id into its two components, a uuid
and a counter.
If you are generating url safe ids, you must pass { urlSafe: true }
as option.
It returns:
{
uuid: '049b7020-c787-41bf-a1d2-a97612c11418',
count: 1
}
This is aliased as instance.decode
.
License
MIT